<template>
  <div class="login">
    <div class="form">
      <p>
        账号:
        <input type="text" v-model="userInfo.username" />
      </p>
      <p>
        密码:
        <input type="password" v-model="userInfo.password" />
      </p>
      <p>
        <button @click="btnlogin">登录</button>
        <button @click="btnregister">注册</button>
      </p>
    </div>
  </div>
</template>

<script>
import { login, register } from "../../api/index";

export default {
  name: "login",
  data() {
    return {
      userInfo: {
        username: "",
        password: ""
      }
    };
  },

  methods: {
    btnlogin() {
      login(this.userInfo).then(res => {
        if (res.data.code === 1) {
          sessionStorage.setItem("token", res.data.token);
          sessionStorage.setItem("name", res.data.datas[0].name);
          // sessionStorage.setItem("username", res.data.datas[0].username);
          sessionStorage.setItem("img", res.data.datas[0].img);

          this.$router.push("/");
          // alert("登录成功");
          this.$toast(res.data.msg);
        } else {
          this.$toast(res.data.msg);
        }
      });
    },
    btnregister() {
      register(this.userInfo).then(res => {
        if (res.data.code === 1) {
          this.$toast(res.data.msg);
        } else {
          this.$toast(res.data.msg);
        }
      });
    }
  }
};
</script>

<style lang="scss" scoped>
.login {
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;
  background-image: url("https://img1.baidu.com/it/u=177087849,1687477285&fm=26&fmt=auto&gp=0.jpghttps://img1.baidu.com/it/u=177087849,1687477285&fm=26&fmt=auto&gp=0.jpg");

  .form {
    width: 80%;
    height: 100%;
    // margin: 60% auto;
    padding-left: 25%;
    padding-top: 40%;
    p {
      padding-top: 10px;
    }
    p:nth-child(3) {
      margin: 0 auto;
      width: 60%;
      height: 40px;
      display: flex;
      justify-content: space-around;
      button {
        border-radius: 20px;
        margin: 10px auto;
        width: 60px;
        height: 40px;
      }
      button:first-child {
        background-color: rgb(126, 144, 245);
        border: none;
      }
      button:nth-child(2) {
        background-color: rgb(100, 212, 66);
        border: none;
      }
    }
  }
}
</style>